package com.naiterui.ehp.bs.b2c.product.controller.inner;

import com.naiterui.common.repository.page.PageVO;
import com.naiterui.ehp.bp.bo.b2c.ProductBoxBO;
import com.naiterui.ehp.bp.bo.b2c.ProductDefaultUsageBO;
import com.naiterui.ehp.bp.bo.b2c.ProductInventoryBO;
import com.naiterui.ehp.bp.bo.b2c.param.InventoryParamBO;
import com.naiterui.ehp.bp.bo.doctor.product.CategoryBO;
import com.naiterui.ehp.bp.bo.doctor.product.CueWordBO;
import com.naiterui.ehp.bp.bo.doctor.product.ProductDetailBO;
import com.naiterui.ehp.bp.bo.doctor.product.ProductInfoBO;
import com.naiterui.ehp.bp.bo.doctor.product.ProductSearchParamBO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bp.support.utils.JsonMapper;
import com.naiterui.ehp.bs.b2c.order.controller.OrderController;
import com.naiterui.ehp.bs.b2c.product.exception.ProductBusinessException;
import com.naiterui.ehp.bs.b2c.product.exception.ProductExceptionCodes;
import com.naiterui.ehp.bs.b2c.product.service.IInnerProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 商品信息内部接口
 *
 * @author gongbaoqiang
 * @date 2019/5/17 15:05
 * @since 1.0.0
 */
@Api(tags = {SwaggerTag.DOCTOR + SwaggerTag.MEDICINE_DRUG, SwaggerTag.CMS + SwaggerTag.MEDICINE_DRUG})
@RequestMapping("b2c/inner/product")
@RestController
public class InnerProductController {
    private static final Logger LOGGER = LoggerFactory.getLogger(OrderController.class);

    @Autowired
    private IInnerProductService innerProductService;

    /**
     * <ul>
     * <li>根据商品skuid获取商品列表，参数","隔开</li>
     * <li>冗余接口，兼容处方服务的原逻辑，原接口参数为pid</li>
     * <li>内部服务有关新的商品信息接口重新定</li>
     * </ul>
     *
     * @param skuIds
     *
     * @return
     *
     * @throws ProductBusinessException
     * @Author gongbaoqiang
     * @Date 2019/5/17 15:05
     */
    @ApiOperation(value = "根据商品skuid获取商品列表，参数,隔开")
    @ApiImplicitParams({@ApiImplicitParam(name = "skuIds", value = "药品ID", paramType = "path", dataType = "string")})
    @PostMapping(value = "/list")
    public ResponseVO<List<ProductBoxBO>> detailList(String skuIds) throws ProductBusinessException {
        if (skuIds == null) {
            LOGGER.warn("根据商品id获取商品列表,请求参数为空");
            throw new ProductBusinessException(ProductExceptionCodes.PARAM_ERROR);
        }
        LOGGER.info("根据商品id获取商品列表,请求参数 skuIds: {}", skuIds);
        List<ProductBoxBO> detailBOList = this.innerProductService.getProductList(skuIds);
        return PublicService.returnResponseVO(ProductExceptionCodes.SUCCESS, detailBOList);
    }

    /**
     * <ul>
     * <li>根据商品库存信息</li>
     * <li>冗余接口，兼容处方服务的原逻辑</li>
     * <li>内部服务有关新的商品库存接口重新定义</li>
     * <li>todo: 各服务代码修改时参数修改为InventoryParamBO，删除unionId</li>
     */
    @ApiOperation(value = "根据商品库存信息")
    @PostMapping(value = "/inventory")
    public ResponseVO<List<ProductInventoryBO>> inventory(String goods) throws ProductBusinessException {
        LOGGER.info("[库存信息] start——根据商品skuid获取商品库存,请求参数 goods: {}", goods);
        if (StringUtils.isEmpty(goods)) {
            LOGGER.warn("[库存信息]——根据商品skuid获取商品库存,请求缺少参数 goods: {}", goods);
            throw new ProductBusinessException(ProductExceptionCodes.PARAM_ERROR);
        }
        List<InventoryParamBO> goodsList = JsonMapper.toList(goods, InventoryParamBO.class);

        List<ProductInventoryBO> inventoryVOList = this.innerProductService.getInventoryInfoList(goodsList);
        LOGGER.info("[库存信息] end——根据商品skuid获取商品库存结果 inventoryVOList: {}", inventoryVOList);
        return PublicService.returnResponseVO(ProductExceptionCodes.SUCCESS, inventoryVOList);
    }

    /**
     * <ul>
     * <li>根据药品skuIds查询药品的默认用法用量</li>
     * <li>当前数据为空，后续进行补充</li>
     * </ul>
     *
     * @param skuIds
     *
     * @return
     *
     * @throws ProductBusinessException
     */
    @ApiOperation(value = "根据药品skuIds查询药品的默认用法用量")
    @ApiImplicitParams({@ApiImplicitParam(name = "skuIds", value = "药品ID", paramType = "path", dataType = "string")})
    @PostMapping("getDefaultUsage")
    public ResponseVO<List<ProductDefaultUsageBO>> getDefaultUsage(String skuIds) throws ProductBusinessException {
        LOGGER.info("根据药品skuIds,查询药品默认用法用量,请求参数为skuIds:{}", skuIds);
        if (StringUtils.isEmpty(skuIds)) {
            LOGGER.warn("skuIds,查询药品默认用法用量，参数异常,skuIds:{}", skuIds);
            throw new ProductBusinessException(ProductExceptionCodes.PARAM_ERROR);
        }
        List<ProductDefaultUsageBO> list = this.innerProductService.getDefaultUsage(skuIds);
        return PublicService.returnResponseVO(ProductExceptionCodes.SUCCESS, list);
    }

    /**
     * 医生搜索商品
     *
     * @param productSearch param
     *
     * @return BO
     *
     * @throws ProductBusinessException businessException
     */
    @ApiOperation(value = "医生搜索商品")
    @PostMapping("/search")
    public ResponseVO<PageVO<ProductInfoBO>> searchProduct(@RequestBody ProductSearchParamBO productSearch) throws ProductBusinessException {
        LOGGER.info("医生搜索商品信息 {}", productSearch);

        if (productSearch == null) {
            throw new ProductBusinessException(ProductExceptionCodes.PARAM_ERROR);
        }

        /*if (productSearch.getCategoryId() == null && StringUtils.isBlank(productSearch.getKey())) {
            throw new ProductBusinessException(ProductExceptionCodes.PARAM_ERROR);
        }*/
        PageVO<ProductInfoBO> page = this.innerProductService.findProductList(productSearch);

        return PublicService.returnResponseVO(page);
    }

    /**
     * @param skuId skuId
     * @return BO
     * @throws ProductBusinessException businessException
     */
    @ApiOperation(value = "获取药品详情")
    @ApiImplicitParams({@ApiImplicitParam(name = "skuId", value = "药品ID", paramType = "query", dataType = "long")})
    @RequestMapping("detail")
    public ResponseVO<ProductDetailBO> getProductDetail(Long skuId) throws ProductBusinessException {
        LOGGER.info("根据skuId:{} 获取商品详情", skuId);
        if (skuId == null) {
            throw new ProductBusinessException(ProductExceptionCodes.PARAM_ERROR);
        }
        return PublicService.returnResponseVO(this.innerProductService.getProductDetail(skuId));
    }

    /**
     * 搜索提示词
     *
     * @param key 搜索词
     *
     * @return 商品全称List
     */
    @ApiOperation(value = "搜索提示词")
    @ApiImplicitParams({@ApiImplicitParam(name = "key", value = "搜索词", paramType = "query", dataType = "string")})
    @RequestMapping("/cueWord/get")
    public ResponseVO<List<CueWordBO>> getCueWord(String key) throws ProductBusinessException {
        LOGGER.info("搜素关键字:{} 获取商品名臣提示词", key);

        if (StringUtils.isBlank(key)) {
            throw new ProductBusinessException(ProductExceptionCodes.PARAM_ERROR);
        }

        List<CueWordBO> cueWords = this.innerProductService.getProductNameList(key);
        return PublicService.returnResponseVO(cueWords);
    }

    /**
     * 获取商品分类
     */
    @ApiOperation(value = "获取商品分类")
    @RequestMapping("/category")
    public ResponseVO<List<CategoryBO>> getCategory() {
        LOGGER.info("查询商品分类");
        return PublicService.returnResponseVO(this.innerProductService.getCategory());
    }
}
